package com.smedi.ismedi.dangeroussourcemanage.core.application.service.impl;

import com.smedi.ismedi.dangeroussourcemanage.core.application.command.DangerousSourceManageCommand;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.aggregatemodel.DangerousSourceManage;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.repository.DangerousSourceManageRepository;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.service.DangerousSourceManageDomainService;
import com.smedi.ismedi.dangeroussourcemanage.core.application.service.DangerousSourceManageApplicationService;
import com.smedi.ismedi.dangeroussourcemanage.core.domain.service.ExportExcelDomainService;
import com.smedi.ismedi.web.starter.util.DozerUtil;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 危险源应用服务实现类
 *
 * @author xienannan
 * @date 2021/08/23
 */
@Service
@AllArgsConstructor
public class DangerousSourceManageApplicationServiceImpl implements DangerousSourceManageApplicationService {
    private final DangerousSourceManageDomainService dangerousSourceManageDomainService;
    private final ExportExcelDomainService exportExcelDomainService;
    private final DangerousSourceManageRepository dangerousSourceManageRepository;

    private final DozerUtil dozerUtil;

    @Override
    public Long addDangerousSource(DangerousSourceManageCommand dangerousSourceManageCommand) {
        DangerousSourceManage dangerousSourceManageInfo = dozerUtil.convertor(dangerousSourceManageCommand, DangerousSourceManage.class);

        DangerousSourceManage dangerousSourceManage;
        if (dangerousSourceManageCommand.getId() == 0) {
            dangerousSourceManage = dangerousSourceManageDomainService.doInsert(dangerousSourceManageInfo);
        } else {
            dangerousSourceManage = dangerousSourceManageDomainService.doUpdate(dangerousSourceManageInfo);
        }

        return dangerousSourceManage.getId();
    }

    @Override
    public ResponseEntity<byte[]> exportDangerousSourceList(List<Long> DangerousSourceIds) {
        List<DangerousSourceManage> dangerousSourceManageList =  dangerousSourceManageRepository.selectDangerousSourceListByDangerousIds(DangerousSourceIds);
        if(dangerousSourceManageList == null){
            return null;
        }
        return exportExcelDomainService.exportDangerousSourceList(dangerousSourceManageList);
    }

    @Override
    public Long deleteDangerousSourceById(List<Long> dangerousSourceIds) {
        return dangerousSourceManageDomainService.doDeleteById(dangerousSourceIds);
    }

}
